﻿// Developer Express Code Central Example:
// World Wide CRM Demo
// 
// The World Wide CRM demo is a next generation application with outstanding
// navigation capabilities and shows a reimagined view of customer contact and
// management. This demo showcases many of the most popular features of the
// WinForms Subscription.
// 
// You can find sample updates and versions for different programming languages here:
// http://www.devexpress.com/example=E3993

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.CrmDemo.Win.Data;
using System.Diagnostics;
using System.IO;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraGrid;
using DevExpress.CrmDemo.Win.Helpers;

namespace DevExpress.CrmDemo.Win.Controls {
    public partial class OppDetail : BasePage {
        public OppDetail() {
            InitializeComponent();
            this.header.BackRequest += new EventHandler(header_BackRequest);
            ColorHelper.SetMenuManager(Controls, this.LookAndFeel.ActiveSkinName, ColorHelper.MainForm);
        }

        void header_BackRequest(object sender, EventArgs e) {
            opportunityBindingSource.EndEdit();
            OnNavRequest(new NavRequestEventArgs(States.Dashboard));
        }

        public Opportunity Opportunity {
            get { return (Opportunity)this.opportunityBindingSource.DataSource; }
            set {
                this.opportunityBindingSource.DataSource = value;
                this.opportunityBindingSource.ResetCurrentItem();

                // TODO - the grid should probably point directly at Opportunity.Activities
                if (value != null)
                    this.activityBindingSource.DataSource = value.Activities;
            }
        }

        private void editor_MouseEnter(object sender, EventArgs e) {
            ((BaseEdit)sender).BackColor = Color.FromArgb(77, 77, 77);
        }

        private void editor_MouseLeave(object sender, EventArgs e) {
            ((BaseEdit)sender).BackColor = Color.FromArgb(51, 51, 51);
        }

        private void OppDetail_Load(object sender, EventArgs e) {
            this.stagesBindingSource.DataSource = DataProvider.Instance.Stages;
            this.teamMemberBindingSource.DataSource = DataProvider.Instance.TeamMembers;
            this.comboBoxEditCategory.Properties.Items.AddRange(DataProvider.Instance.Technologies.ToArray());
            this.comboBoxLeadSource.Properties.Items.AddRange(DataProvider.Instance.LeadSources.ToArray());
            this.comboBoxOppType.Properties.Items.AddRange(DataProvider.Instance.OpportunityTypes.ToArray());
        }

        private void opportunityBindingSource_CurrentItemChanged(object sender, EventArgs e) {
            if (opportunityBindingSource.Current != null) {
                Opportunity opp = (Opportunity)opportunityBindingSource.Current;

                TeamMember teamMember = DataProvider.Instance.TeamMembers.Where(t => t.Id == opp.OwnerId).SingleOrDefault();

                if (teamMember != null)
                    pictureBoxTeamMember.Image = teamMember.Photo;
            }
        }

        private void pictureBoxNewActivity_Click(object sender, EventArgs e) {
            Activity newActivity = DataProvider.Instance.CreateNewActivity(Opportunity);

            using (var dialog = new ActivityEditorForm(newActivity)) {
                if (dialog.ShowDialog() == DialogResult.OK) {
                    Opportunity.Activities.Add(newActivity);
                }
            }
        }

        private void gridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e) {
            Activity activity = (Activity)gridView1.GetRow(e.RowHandle);

            if (activity == null)
                return;

            if (e.Column == colDocuments || e.Column == colDocumentIcon) {
                try {
                    Process.Start(Path.GetFullPath(activity.DocumentPath));
                } catch { }
            } else {
                using (var dialog = new ActivityEditorForm(activity)) {
                    dialog.ShowDialog();
                }
            }
        }

        int? hoverRow;

        private int? HoverRow {
            get { return hoverRow; }
            set {
                if (hoverRow != value) {
                    int? prevHoverRow = hoverRow == null ? GridControl.InvalidRowHandle : hoverRow;

                    hoverRow = value;

                    gridView1.RefreshRow(prevHoverRow ?? GridControl.InvalidRowHandle);

                    if (hoverRow != null)
                        gridView1.RefreshRow((int)hoverRow);
                }
            }
        }

        private void gridView1_MouseMove(object sender, MouseEventArgs e) {
            GridView view = sender as GridView;

            GridHitInfo info = view.CalcHitInfo(new Point(e.X, e.Y));

            HoverRow = info.RowHandle;
        }

        private void gridView1_MouseLeave(object sender, EventArgs e) {
            HoverRow = null;
        }

        private void gridView1_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e) {
            bool hovering = HoverRow != null && e.RowHandle == HoverRow;

            e.Appearance.BackColor = hovering ? Color.FromArgb(68, 75, 95) : Color.FromArgb(51, 51, 51);
        }

        private void xtraScrollableControl1_Click(object sender, EventArgs e) {
            ActiveControl = xtraScrollableControl1;
            xtraScrollableControl1.Focus();
        }
    }
}